﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Weblications.Sango
{
    class BadWordHelper
    {
        private static string default_split_string = "|";
        private static string default_replace_string = "*";

        private int maxWordLength = 0;
        private int minWordLength = int.MaxValue;

        private List<string> hash = new List<string>();//多字检查
        private byte[] fastCheck = new byte[byte.MaxValue];//多字检查
        private BitArray charCheck = new BitArray(char.MaxValue);//单字检查

        public BadWordHelper(string dictionary)
        {
            if (string.IsNullOrEmpty(dictionary))
                return;

            string[] badwords = dictionary.Split(new char[] { '|' });

            foreach (string word in badwords)
            {
                if (string.IsNullOrEmpty(word))
                    continue;

                maxWordLength = Math.Max(maxWordLength, word.Length);

                minWordLength = Math.Max(minWordLength, word.Length);

                for (int i = 0; i < 7 && i < word.Length; i++)
                {
                    fastCheck[word[i]] |= (byte)(1 << i);
                }

                for (int i = 7; i < word.Length; i++)
                {
                    fastCheck[word[i]] |= 0x80;
                }

                if (word.Length == 1)
                {
                    charCheck.Set(word[0], true);
                }
                else
                {
                    hash.Add(word);
                }
            }
        }

        public bool hasBadWord(string text)
        {
            if (string.IsNullOrEmpty(text))
                return false;

            int index = 0;

            while (index < text.Length)
            {

                if ((fastCheck[text[index]] & 1) == 0)
                {
                    while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0)
                        ;
                }

                //单字节检测
                if (minWordLength == 1 && charCheck.Get(text[index]))
                {
                    return true;
                }

                //多字节检测
                for (int j = 1; j <= Math.Min(maxWordLength, text.Length - index - 1); j++)
                {
                    //快速排除
                    if ((fastCheck[text[index + j]] & (1 << Math.Min(j, 7))) == 0)
                    {
                        break;
                    }
                    if (j + 1 >= minWordLength)
                    {
                        string sub = text.Substring(index, index + j + 1);

                        if (hash.Contains(sub))
                        {
                            return true;
                        }
                    }
                }
                index++;
            }
            return false;
        }

        public string replaceBadWord(string text)
        {
            int index = 0;

            for (index = 0; index < text.Length; index++)
            {
                if ((fastCheck[text[index]] & 1) == 0)
                {
                    while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0)
                        ;
                }

                //单字节检测
                if (minWordLength == 1 && charCheck.Get(text[index]))
                {
                    text = text.Replace(text[index], default_replace_string[0]);

                    continue;
                }

                //多字节检测
                for (int j = 1; j <= Math.Min(maxWordLength, text.Length - index - 1); j++)
                {
                    //快速排除
                    if ((fastCheck[text[index + j]] & (1 << Math.Min(j, 7))) == 0)
                    {
                        break;
                    }
                    if (j + 1 >= minWordLength)
                    {
                        string sub = text.Substring(index, index + j + 1);

                        if (hash.Contains(sub))
                        {
                            //替换字符操作
                            string rp = default_replace_string.PadRight((j + 1), default_replace_string[0]);
                            text = text.Replace(sub, rp);
                            //记录新位置
                            index += j;
                            break;
                        }
                    }
                }
            }

            return text;
        }
    }
}
